package com.foreveross.mofang.serviceconsole.application.dto;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.validation.constraints.Digits;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.apache.commons.lang.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import com.foreveross.mofang.serviceconsole.core.Group;


public class GroupDTO extends BaseDTO<Group>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Size(max = 36, message = "编号不能超过36个字符")
	private String sn;
	
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date lastUpdate;
	
	@NotNull(message = "名称不能为空")
	@Size(max = 30, message = "名称不能超过30个字符")
	private String name;
	
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date createDate;
	
	@NotNull(message = "排序不能为空")
	@Digits(integer = 2, fraction = 2, message = "顺序号必需小数据点后两位!")
	private Float sortNum;
	
	// 创建人
	private String createUserName;
	
	private String createUserAccount;
	
	// 最后更新人
	private String lastUpdateUserName;
	
	private String lastUpdateUserAccount;
	
	private List<GroupDTO> children;
	
	private GroupDTO parant;
	
	private boolean isConversionChilds = true;
	
	public GroupDTO() {
		super();
	}

	public GroupDTO(boolean isConversionChilds) {
		super();
		this.isConversionChilds = isConversionChilds;
	}

	public String getSn() {
		return sn;
	}

	public void setSn(String sn) {
		this.sn = sn;
	}

	public Date getLastUpdate() {
		return lastUpdate;
	}

	public void setLastUpdate(Date lastUpdate) {
		this.lastUpdate = lastUpdate;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Float getSortNum() {
		return sortNum;
	}

	public void setSortNum(Float sortNum) {
		this.sortNum = sortNum;
	}

	public String getCreateUserName() {
		return createUserName;
	}

	public void setCreateUserName(String createUserName) {
		this.createUserName = createUserName;
	}

	public String getCreateUserAccount() {
		return createUserAccount;
	}

	public void setCreateUserAccount(String createUserAccount) {
		this.createUserAccount = createUserAccount;
	}

	public String getLastUpdateUserName() {
		return lastUpdateUserName;
	}

	public void setLastUpdateUserName(String lastUpdateUserName) {
		this.lastUpdateUserName = lastUpdateUserName;
	}

	public String getLastUpdateUserAccount() {
		return lastUpdateUserAccount;
	}

	public void setLastUpdateUserAccount(String lastUpdateUserAccount) {
		this.lastUpdateUserAccount = lastUpdateUserAccount;
	}

	public List<GroupDTO> getChildren() {
		return children;
	}

	public void setChildren(List<GroupDTO> children) {
		this.children = children;
	}

	public GroupDTO getParant() {
		return parant;
	}

	public void setParant(GroupDTO parant) {
		this.parant = parant;
	}

	@Override
	public void domain2Dto(Group group) {
		super.setId(group.getId());
		this.setSn(group.getSn());
		this.setName(group.getName());
		this.setSortNum(group.getSortNum());
		this.setCreateDate(group.getCreateDate());
		this.setCreateUserAccount(group.getUser().getUserAccount());
		this.setCreateUserName(group.getUser().getName());
		this.setLastUpdateUserAccount(group.getLastUpdateUser().getUserAccount());
		this.setLastUpdateUserName(group.getLastUpdateUser().getName());
		List<Group> children = group.getChildren();
		
		if(isConversionChilds && children != null) {
			List<GroupDTO> childsDTO = new ArrayList<GroupDTO>();
			GroupDTO childDTO = null;
			
			for (Group child : children) {
				if (child.getRemoved()) {
					continue;
				}
				
				childDTO = new GroupDTO();
				childDTO.domain2Dto(child);
				childsDTO.add(childDTO);
			}
			
			this.setChildren(childsDTO);
		}
	}

	@Override
	public void dto2Domain(Group group) {
		group.setId(this.getId());
		
		if (StringUtils.isNotBlank(this.sn)) {
			group.setSn(this.sn);
		}
		
		if (StringUtils.isNotBlank(this.name)) {
			group.setName(this.name);
		}
		
		if(sortNum != null) {
			group.setSortNum(this.sortNum);
		}
		
		if (parant != null && parant.getId() != null) {
			group.setParant(Group.get(Group.class, parant.getId()));
		}
		
	}
}